//
//  AccountController.m
//  MClient
//
//  Created by  史正烨 on 09-10-31.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "AccountController.h"
#import "MCTableCell.h"
#import "LoginController.h"

@interface AccountTableCell : MCTableCell {
	UILabel *lbValue1;//内容1
	UILabel *lbValue2;//内容2
}
@property(nonatomic, retain)UILabel *lbValue1;
@property(nonatomic, retain)UILabel *lbValue2;
@end

#pragma mark -- AccountTableCell -- 
@implementation AccountTableCell
@synthesize lbValue1, lbValue2;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier   {
    if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]) {
		//标题
		UILabel* lbTitle1 = [[UILabel alloc] initWithFrame:CGRectMake(5, 10, 40, 25)];
		lbTitle1.textAlignment = UITextAlignmentLeft;
		lbTitle1.font = [UIFont boldSystemFontOfSize:16];
		lbTitle1.textColor = [UIColor grayColor];
		lbTitle1.backgroundColor =[UIColor clearColor];
		lbTitle1.text = @"会员:";
		[self.contentView addSubview:lbTitle1];
		[lbTitle1 release];
		//内容
		lbValue1 = [[UILabel alloc] initWithFrame:CGRectMake(50, 10, 100, 25)];
		lbValue1.textAlignment = UITextAlignmentLeft;
		lbValue1.font = [UIFont boldSystemFontOfSize:16];
		lbValue1.textColor = [UIColor blackColor];
		lbValue1.backgroundColor =[UIColor clearColor];
		[self.contentView addSubview:lbValue1];
		
		//标题
		UILabel* lbTitle2 = [[UILabel alloc] initWithFrame:CGRectMake(140, 10,40, 25)];
		lbTitle2.textAlignment = UITextAlignmentLeft;
		lbTitle2.font = [UIFont boldSystemFontOfSize:16];
		lbTitle2.textColor = [UIColor grayColor];
		lbTitle2.backgroundColor =[UIColor clearColor];
		lbTitle2.text = @"用户:";
		[self.contentView addSubview:lbTitle2];
		[lbTitle2 release];

		//内容
		lbValue2 = [[UILabel alloc] initWithFrame:CGRectMake(190, 10, 100, 25)];
		lbValue2.textAlignment = UITextAlignmentLeft;
		lbValue2.font = [UIFont boldSystemFontOfSize:16];
		lbValue2.textColor = [UIColor blackColor];
		lbValue2.backgroundColor =[UIColor clearColor];
		[self.contentView addSubview:lbValue2];
	}
	
    return self;
}
- (void)dealloc
{
	[lbValue1 release];
	[lbValue2 release];
	[super dealloc];
}
@end

#pragma mark -- AccountController --

//@interface AccountController (Pri)
//NSMutableDictionary *serverList;
//NSString *currentUser;
//@end

@implementation AccountController

- (int)identiferId {
	return 0;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[super viewDidLoad];

	self.tableview.backgroundColor = [UIColor clearColor];
	
	serverList = [[NSMutableDictionary alloc] init];
	[serverList addEntriesFromDictionary:self.userDefaults.serverList];
	
	MCLog(@"--AccountController-viewDidLoad-serverList=%@", serverList);
	// 添加右工具条按钮
	if ([[serverList allKeys] count] > 0) {
		[self rightBarButton:@"删除" withStyle:UIBarButtonItemStyleDone selector:@selector(toggleEdit:)];
	}
	
	self.title = @"已有帐号";
	
	self.addHelpButton = YES;
	self.helpUrl = @"/iphone/help/chapter02-02.html";
	
	currentUser = [[NSString stringWithFormat:@"%@||%@",self.userDefaults.current_PartyId, self.userDefaults.current_UserId] retain];
}


- (void)dealloc {
	[currentUser release];
	[serverList release];
    [super dealloc];
}

#pragma mark ---- UITableView方法 ----
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return [[serverList allKeys] count];
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSString *key = [[serverList allKeys] objectAtIndex:section];
	return [[serverList objectForKey:key] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{	
	if (self.userDefaults.needServerEditor) {
		return [[serverList allKeys] objectAtIndex:section];
	}else {
		return nil;
	}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	AccountTableCell *cell = (AccountTableCell*)[super tableCell:tableView withClass:[AccountTableCell class] withIdentifier:@"AccountTableCell"];
	cell.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7];
	// server 地址
	NSString *serverUrl = [[serverList allKeys] objectAtIndex:indexPath.section];
	NSDictionary* userList = [serverList objectForKey:serverUrl]; 
	// 格式为：会员.用户
	NSString *user = [[userList allKeys] objectAtIndex:indexPath.row];
	NSArray *splite = [user componentsSeparatedByString:@"||"];
	cell.lbValue1.text = [splite objectAtIndex:0];
	cell.lbValue2.text = [splite objectAtIndex:1];
	
	if ([user isEqualToString:currentUser]) {
		cell.accessoryType = UITableViewCellAccessoryCheckmark;
		cell.editingAccessoryType = UITableViewCellAccessoryCheckmark;
	}
	else {
		cell.accessoryType = UITableViewCellAccessoryNone;
		cell.editingAccessoryType = UITableViewCellAccessoryNone;
	}
	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	// server 地址
	NSString *serverUrl = [[serverList allKeys] objectAtIndex:indexPath.section];
	NSDictionary* userList = [serverList objectForKey:serverUrl];
	NSString *user = [[userList allKeys] objectAtIndex:indexPath.row];
	NSArray *splite = [user componentsSeparatedByString:@"||"];
	
	self.userDefaults.current_PartyId = [splite objectAtIndex:0];
	self.userDefaults.current_UserId = [splite objectAtIndex:1];
	self.userDefaults.current_ServerUrl = serverUrl;
	
	LoginController* controller = (LoginController*)[self.navigationController.viewControllers objectAtIndex:0];
	[controller reloadFieldValues];
	[self.navigationController popViewControllerAnimated:YES];
}

- (void)tableView:(UITableView *)tableView 
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {

	// server 地址
	NSString *serverUrl = [[serverList allKeys] objectAtIndex:indexPath.section];
	NSMutableDictionary* userList = [NSMutableDictionary dictionary];
	[userList addEntriesFromDictionary:[serverList objectForKey:serverUrl]];
	NSString *user = [[userList allKeys] objectAtIndex:indexPath.row];
	
	[userList removeObjectForKey:user];
	if ([[userList allKeys] count] == 0) {
		[serverList removeObjectForKey:serverUrl];
	} else {
		[serverList setObject:userList forKey:serverUrl];
	}

	self.userDefaults.serverList = serverList;
	
	if ([[serverList allKeys] count] == 0) {
		self.rightBarButtonItem = nil;
	}
	
	[self.tableview reloadData];
}
#pragma mark ---- 事件处理方法 ----
- (IBAction)toggleEdit:(id)sender {
	[self.tableview setEditing:!self.tableview.editing animated:YES];
	self.rightBarButtonItemTitle = self.tableview.editing?@"完成":@"删除";
}

@end
